package com.fencing_squad.homo.slice;

import com.alibaba.fastjson.JSONObject;
import com.fencing_squad.homo.ResourceTable;
import com.fencing_squad.homo.list.PostUser;
import com.fencing_squad.homo.list.Product;
import com.fencing_squad.homo.list.ProductProvider;
import com.fencing_squad.homo.util.CustomComponent;
import com.fencing_squad.homo.util.Data;
import com.fencing_squad.homo.util.Network;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;

import java.util.ArrayList;

public class StoreSlice extends AbilitySlice {
    private ProductProvider provider;
    private String name = "";
    private String phone = "";
    private String address = "";
    private String addressDetail = "";
    private Product product;
    private CommonDialog window;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent((ComponentContainer) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_store, null, false));

        initConfig();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void initConfig() {
        ListContainer container = (ListContainer) findComponentById(ResourceTable.Id_productList);
        provider = new ProductProvider(this, (position) -> {
            product = (Product) provider.getItem(position);
            if (!Data.Account.isLogin) {
                present(new GuideSlice(), new Intent());
                return;
            }
            if (product.getNumberInt() <= 0) {
                CustomComponent.getDialog(this, "兑换商品数需大于0！", null, null).show();
                return;
            }
            if (product.getTotal() > Data.Account.point) {
                CustomComponent.getDialog(this, "积分不足！", null, null).show();
                return;
            }
            window = CustomComponent.getOrder(getContext(), product, this::setName, this::setPhone, this::setAddress, this::setAddressDetail, this::confirm);
            window.show();
        });
        container.setItemProvider(provider);

        container.addScrolledListener(new Component.ScrolledListener() {
            @Override
            public void onContentScrolled(Component component, int x, int y, int oldX, int oldY) {
                System.out.println("scroll");
                System.out.println(component instanceof ListContainer);
                System.out.println(y);
            }
        });

        Text barTitle = (Text) findComponentById(ResourceTable.Id_barTitle);
        Component barBack = findComponentById(ResourceTable.Id_barBack);
        barTitle.setText("HOMO商城");
        barBack.setClickedListener(component -> {
            terminate();
        });

        Network.get("/store", this::resolve, this::reject);
    }

    private void resolve(JSONObject object) {
        provider.append(Product.getFromJSONArray(object.getJSONArray("data")));
    }

    private void reject(int code, String message) {
        if(code == 404) {
            CustomComponent.getDialog(this, message, null, null).show();
        }
        else {
            System.out.println(message);
        }
    }

    public static ArrayList<Product> test() {
        ArrayList<Product> list = new ArrayList<>();
        for(int i=0; i<10; i++) {
            System.out.println(i);
            Product product = new Product();
            product.setName("哼哼哼啊啊啊啊啊");
            product.setImage("");
            product.setItemNum(i);
            product.setPoint(i * 2);
            list.add(product);
        }
        return list;
    }

    public void setName(String s) {
        name = s;
    }

    public void setPhone(String s) {
        phone = s;
    }

    public void setAddress(String s) {
        address = s;
    }

    public void setAddressDetail(String s) {
        addressDetail = s;
    }

    public void confirm() {
        if (name.equals("") || phone.equals("") || address.equals("") || addressDetail.equals("")) {
            CustomComponent.getDialog(this, "请将信息填写完整！", null, null).show();
            return;
        }
        JSONObject object = new JSONObject();
        object.put("item_id", product.getItemId());
        object.put("number", product.getNumberInt());
        object.put("user_name", name);
        object.put("phone", phone);
        object.put("position", address);
        object.put("detail", addressDetail);

        Network.postJson("/store", object.toJSONString(), this::purchaseResolve, this::reject);
    }

    private void purchaseResolve(JSONObject object) {
        if (window != null) {
            window.hide();
        }
        Data.Account.point = object.getIntValue("data");
        CustomComponent.getToastDialog(this, "兑换成功").show();
    }
}
